#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#define LEN 1024*8

int main(int argc, char *argv[]){
    if(argc < 3 ){
        printf("[%s]:Usage:%s <file_name> <string>\n",argv[0], argv[0]);
        return -1;
    }

    int fd = open(argv[1], O_RDWR);
    if(fd < 0){
        perror("open");
        return -1;
    }

    char *buff = mmap (NULL, (size_t)LEN, PROT_READ | PROT_WRITE,
		   MAP_SHARED, fd, 0);
    if(buff == MAP_FAILED){
        perror("mmap");
        close(fd);
        return -1;
    }
    printf("mmap address:0x%x\n",buff);

    if (strncpy(buff, argv[2], LEN - 1) == NULL) {
        perror("strncpy");
        close(fd);
        munmap(buff, LEN);
        return -1;
    }
    buff[LEN - 1] = '\0';

    char buf_read[1024];
    if(read(fd, buf_read, sizeof(buf_read)) < 0){
        perror("read");
        close(fd);
        munmap(buff, LEN);
        return -1;
    }
    printf("read from buff %s\n", buf_read);

    while(1){
        sleep(100);
    }

    munmap(buff,LEN);
    close(fd);  // Clean up file descriptor
    return 0;
}